/*
 * BackupDataHandler.java
 *
 * Created on April 7, 2007, 9:54 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package edu.byu.isys413.group1E.handlers;

import edu.byu.isys413.group1E.data.BackupDataBO;
import edu.byu.isys413.group1E.data.BackupDataDAO;
import edu.byu.isys413.group1E.data.CustomerBO;
import edu.byu.isys413.group1E.data.IconsDAO;
import java.sql.SQLException;
import java.util.ArrayList;

/** This class handles processing needed for BackupData transactions.
 *
 * @author Travis Kulbeth
 */
public class BackupDataHandler {
    
    private static BackupDataHandler instance = null;
    
    /** Creates a new instance of BackupDataHandler */
    public BackupDataHandler() {
    }
    
    /**
     *gets you an instance of BackupDataHandler 
     *
     * @return instance of BackupDataHandler
     */
    public static synchronized BackupDataHandler getInstance() {
        if (instance == null) {
            instance = new BackupDataHandler();
        }
        return instance;
    }
    
    /**
     * This method returns an array list of BackupData objects that pertain
     * to a given customer (member)
     * 
     * @return an array list of the BackupData objects
     * @param cust contains the CustomerBO to extract information from.
     */
    public ArrayList<BackupDataBO> readListByCust(CustomerBO cust){
        ArrayList<BackupDataBO> list = new ArrayList<BackupDataBO>();
        try {
            list = BackupDataDAO.getInstance().readByMember(cust.getCustMembership().getMembershipID());
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        }
        return list;
    }
    
    /**
     * This method returns an array list of filenames for icons that correspond with the files
     *listed in the BackupDataBO list.  The corresponding icons in this method are retrieved by 
     *the file extensions, but a similar comparison can be made between the tables using the file content type.
     *
     *I chose to use the file extensions due to the large number of possible file types.
     *
     *If the extension is not included in the current Icons table, this method then associates the 
     *file in the list with an icon for an unknown file type.
     * 
     * @return an array list of the BackupData objects
     * @param bkDataList contains a list of BackupData used to retrieve icon filenames.
     */
    public ArrayList<String> getIcons(ArrayList<BackupDataBO> bkDataList){
        ArrayList<String> iconList = new ArrayList<String>();
        try {
            for(int x=0;x<bkDataList.size();x++){
                String filename = IconsDAO.getInstance().readByExt(bkDataList.get(x).getExt());
                if(filename == null){
                    iconList.add("unknown_icon");
                }else{
                    iconList.add(filename);
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        } catch (DataException ex) {
            ex.printStackTrace();
        } catch (ConnectionPoolException ex) {
            ex.printStackTrace();
        }
        return iconList;
    }
}
